package edu.scau.mis.pos.service.impl;

import edu.scau.mis.pos.domain.OrderItem;
import edu.scau.mis.pos.mapper.OrderItemMapper;
import edu.scau.mis.pos.mapper.OrderMapper;
import edu.scau.mis.pos.service.IOrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderItemServiceImpl implements IOrderItemService {
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public int insertOrderItem(OrderItem orderItem) {
        return orderItemMapper.insertOrderItem(orderItem);
    }

    @Override
    public int deleteOrderItemById(Long orderItemId) {
        return orderItemMapper.deleteOrderItemById(orderItemId);
    }

    @Override
    public int updateOrderItem(OrderItem orderItem) {
        return orderItemMapper.updateOrderItem(orderItem);
    }

    @Override
    public List<OrderItem> selectOrderItemsByOrderId(String orderId) {
        return orderItemMapper.selectOrderItemsByOrderId(orderId);
    }

    public boolean clearOrderPriceAndQuantity(String orderId) {
        try {
            // 更新订单明细中的价格、数量、原价为 0
            orderItemMapper.updateOrderPriceAndQuantity(orderId);
            // 更新订单的总价、件数等
            orderMapper.updateOrderPriceAndQuantity(orderId);
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    @Override
    public List<OrderItem> getOrderItemsByOrderId(String orderId) {
        // 调用 Mapper 的方法来获取订单明细
        return orderItemMapper.selectOrderItemsByOrderId(orderId);
    }
}

